package ua.com.infinite.mobile.slidingmenu;

import ua.com.infinite.mobile.App;
import ua.com.infinite.mobile.MainActivity;
import ua.com.infinite.mobile.R;
import ua.com.infinite.mobile.utils.imageloader.ImageLoader;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class BaseMenuFragment extends Fragment implements OnClickListener, OnItemClickListener {

	private String[] menuItemNames;

	private ImageLoader imageLoader;

	private ImageView userAvatar;
	private TextView userName;
	private TextView ballance;
	private Button btn_LogOff;
	private ListView listMenu;
	private MainActivity main;

	public static BaseMenuFragment newInstance() {
		return new BaseMenuFragment();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View menuView = inflater.inflate(R.layout.base_menu_list, null);
		userAvatar = (ImageView) menuView.findViewById(R.id.menu_user_avatar);
		userName = (TextView) menuView.findViewById(R.id.menu_user_name);
		ballance = (TextView) menuView.findViewById(R.id.menu_user_ballance);
		btn_LogOff = (Button) menuView.findViewById(R.id.menu_btn_log_off);
		listMenu = (ListView) menuView.findViewById(R.id.menu_list);
		listMenu.setOnItemClickListener(this);
		btn_LogOff.setOnClickListener(this);
		return menuView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		imageLoader = new ImageLoader(getActivity().getApplicationContext());
		imageLoader.DisplayImage(App.getUser().getAvatar(), userAvatar);
		menuItemNames = getResources().getStringArray(R.array.menu_items);
		userName.setText(App.getUser().getFirstName() + " " + App.getUser().getLastName());

		ballance.setText(App.getUser().getBallance().toString());
		BaseMenuAdapter adapter = new BaseMenuAdapter(getActivity());
		for (int i = 0; i < menuItemNames.length; i++) {
			adapter.add(new BaseMenuItem(menuItemNames[i], android.R.drawable.btn_star, i));
		}
		listMenu.setAdapter(adapter);
	}

	public void setMain(MainActivity main) {
		this.main = main;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.menu_btn_log_off:
			userName.setText("Shysh Pavel");
			App.logOff(getActivity().getApplicationContext());
			Toast.makeText(getActivity(), "Log off", Toast.LENGTH_SHORT).show();
			getActivity().setResult(Activity.RESULT_OK);
			getActivity().finish();
			break;

		default:
			break;
		}

	}

	public class BaseMenuItem {
		public String tag;
		public int iconRes;
		public int countOfNotifications;

		public BaseMenuItem(String tag, int iconRes, int countOfNotifications) {
			this.tag = tag;
			this.iconRes = iconRes;
			this.countOfNotifications = countOfNotifications;
		}
	}

	public class BaseMenuAdapter extends ArrayAdapter<BaseMenuItem> {
		public BaseMenuAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.base_menu_row, null);
			}
			ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
			TextView title = (TextView) convertView.findViewById(R.id.row_title);
			TextView notif = (TextView) convertView.findViewById(R.id.row_notification);

			icon.setImageResource(getItem(position).iconRes);
			title.setText(getItem(position).tag);
			notif.setText("" + getItem(position).countOfNotifications);

			return convertView;

		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (main != null) {
			switch (arg2) {
			case 0:
				main.showMenuAction("News");
				break;
			case 1:
				main.showMenuAction("CheckIn");
				break;
			case 2:
				main.showMenuAction("Actions");
				break;
			default:
				break;
			}

		}
	}
}
